#!/bin/bash

while true; do
    read -p "请输入无人机数量（1-8）: " VEHICLE_NUM
    if [[ $VEHICLE_NUM =~ ^[1-8]$ ]]; then
        break
    else
        echo "错误：请输入1-8之间的数字"
    fi
done

# 生成动态启动脚本
LAUNCH_SCRIPT="yijianqifei.sh"

cat > "${LAUNCH_SCRIPT}" <<EOF
#!/bin/bash

# 基础环境配置
SOURCE_CMD="source /opt/ros/foxy/setup.bash; source /home/nvidia/soft_simulate/install/setup.bash"

# 启动定位和避障节点（全局单例）
gnome-terminal -- bash -c "\${SOURCE_CMD}; ros2 launch uav_swarms getlocalpos_node_launch.py vehicle_num:=${VEHICLE_NUM}; exec bash"
gnome-terminal -- bash -c "\${SOURCE_CMD}; ros2 launch uav_swarms avoid_node_launch.py vehicle_num:=${VEHICLE_NUM}; exec bash"

# 动态生成通信节点
for ((i=0; i<${VEHICLE_NUM}; i++)); do
    gnome-terminal -- bash -c "\${SOURCE_CMD}; ros2 launch uav_swarms communication_multirotor_node\${i}_launch.py; exec bash"
done

echo "成功启动 ${VEHICLE_NUM} 个无人机节点"
EOF

# 设置执行权限
chmod +x "${LAUNCH_SCRIPT}"

echo "======================================================"
echo "动态启动脚本已生成：${LAUNCH_SCRIPT}"
echo "执行以下命令启动集群："
echo "./${LAUNCH_SCRIPT}"
echo "======================================================"